package business.application.test_bed;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;


import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.apache.log4j.Category;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.io.FileInputStream;
//import java.io.IOException;

import org.kth.dks.JDHT;
import org.kth.dks.JDHTReputated;
import org.kth.dks.dks_exceptions.DKSIdentifierAlreadyTaken;
import org.kth.dks.dks_exceptions.DKSRefNoResponse;
import org.kth.dks.dks_exceptions.DKSTooManyRestartJoins;

import business.application.DBSimulator;
import business.search_eng.PostingList;

public class testpipeline {
	
	public static void main(String[] args) throws SecurityException, IOException {
		FileHandler handy = new FileHandler("pt_n"+args[0]+"_d"+args[1]+".log");
		if(args.length <3) {
			System.out.println("USAGE: nodes docs port");
		}
			
		handy.setFormatter(new SimpleFormatter());
		Logger log = Logger.getLogger("log_file");
		log.addHandler(handy);
		  
		System.out.println(log.getName());

		String ref = DBSimulator.load(log,Integer.parseInt(args[0]), Integer.parseInt(args[1]),Integer.parseInt(args[2]));
		log.info("DONE");
		DBClient.test(ref);
	}
	
	public static void main2(String[] args) throws SecurityException, IOException, InterruptedException {
		FileHandler handy = new FileHandler("pt_n"+args[0]+"_d"+args[1]+".log");
			
		handy.setFormatter(new SimpleFormatter());
		Logger log = Logger.getLogger("log_file");
		log.addHandler(handy);
		  
		System.out.println(log.getName());

		//String ref = DBSimulator.load(log,Integer.parseInt(args[0]), Integer.parseInt(args[1]));
		log.info("DONE");
		
		DBSimulator simulator = new DBSimulator(log, Integer.parseInt(args[0]), Integer.parseInt(args[1]),Integer.parseInt(args[2]));
		JDHTReputated node = null;
		try {
			simulator.getCollection();
			simulator.analyzeCollection();
			node = simulator.createOverlayNetwork();
			simulator.indexCollection(node);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DKSTooManyRestartJoins e) {
			e.printStackTrace();
		} catch (DKSIdentifierAlreadyTaken e) {
			e.printStackTrace();
		} catch (DKSRefNoResponse e) {
			e.printStackTrace();
		}
		
		ArrayList<DBClient> clients = new ArrayList<DBClient>();
		
		int i=0;
		Iterator<String> authorIt = simulator.aunodeList.keySet().iterator();
		int totalkeys = simulator.aunodeList.keySet().size();
		int percentage = (int) Math.floor(Double.parseDouble(args[2])/totalkeys);
	
		while(authorIt.hasNext() && i<percentage) {
			JDHT ref = simulator.aunodeList.get(authorIt.next());
			DBClient client = new DBClient(ref);
			Thread thread = new Thread(client);
			clients.add(client);
			thread.start();
			thread.sleep(i*1000);
			i++;
		}
		
		int max=Integer.MIN_VALUE, min=Integer.MAX_VALUE, avg=0;
		
		Iterator<DBClient> clientIt = clients.iterator();
		while(clientIt.hasNext()) {
			DBClient client = clientIt.next();
			if(client.max > max) max = (int) client.max;
			if(client.min > min) min = (int) client.min;
			avg+=client.avg;
			
		}
		avg = avg/clients.size();

		System.out.println("MAX "+max+" MIN "+min+" AVG "+avg);
	}
	
}
